package com.hua.yuan.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hua.yuan.R;
import com.hua.yuan.ui.ImageShowActivity;
import com.hua.yuan.ui.ImageTypeActivity;
import com.hua.yuan.ui.adapter.DetailRecycleAdapter;
import com.hua.yuan.ui.module.ImageDetailBean;

/**
 * Created by huangyang on 2018/7/16.
 * 显示具体的信息
 */

public class DetailItemView extends RelativeLayout {

    private Context context;

    private TextView imageTipView;
    private RecyclerView imageRecyclerView;

    private LinearLayout llId1;

    private void init(Context context) {

        this.context = context;

        LayoutInflater.from(context).inflate(R.layout.detail_item_view, this);
        imageTipView = findViewById(R.id.imageTipView);
        imageRecyclerView = findViewById(R.id.imageRecyclerView);
        llId1 = findViewById(R.id.llId1);


    }

    public DetailItemView(Context context) {
        super(context);
        init(context);
    }

    public DetailItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DetailItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @SuppressLint("NewApi")
    public DetailItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }


    /**
     * 设置数据
     *
     * @param bean 对象
     */
    public void setData(final ImageDetailBean bean) {

        if (bean == null) {
            return;
        }

        final int type = bean.getType();
        switch (type) {
            case 0:
                imageTipView.setText("最新推介");
                break;
            case 1:
                imageTipView.setText("热门图集");
                break;
            case 2:
                imageTipView.setText("其他图集");
                break;
        }


        DetailRecycleAdapter adapter = new DetailRecycleAdapter(context);
        adapter.setList(bean.getList());

        LinearLayoutManager layoutmanager = new LinearLayoutManager(context);
        layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        imageRecyclerView.setLayoutManager(layoutmanager);
        imageRecyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        llId1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                int subtype = 0;
                if (null != bean.getList()) {
                    if (bean.getList().size() > 0) {
                        subtype = bean.getList().get(0).getSubtype();
                    }
                }
                context.startActivity(new Intent(context, ImageTypeActivity.class).putExtra("type", ImageShowActivity.parentType).putExtra("subtype", subtype));
            }
        });

    }

}
